Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: '',
    freight_num: '',
    type:'',
    reason:'',
    size: 30,
    img_list: [],
    uploadImage: '',
    img: {},
    items: [
      { type: '1', value: '退货'},
      { type: '2', value: '换货'}
    ],
    active_: '4',
  },
  // setType: function (e) { 
  //   console.log('radio发生change事件，携带value值为：', e.detail.value);
  //   console.log(typeof e.detail.value); 
  // },
  // 保存订单号
  setOrderId(e) {
    this.setData({
      order_id: e.detail.value
    })
  },
  getData() {
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      self = this
    wx.request({
      url: baseUrl + '/MyOrder/order',
      data: { access_token },
      method: 'GET',
      success: data => {
        console.log(data.data)
        if (data.data.code == 200) {
          self.setData({
            data: data.data.data
          })
        }else{
          wx.navigateBack({
            delta: 1
          })
        } 
      }
    })
  },
  // 输入物流单号
  freight_numInput(e) {
    this.setData({
      freight_num: e.detail.value
    })
  },
  // 保存退/换货类型
  setType(e) {
    console.log('退 /换货类型为',e.detail.value);
    console.log('其数据类型为',typeof e.detail.value);
    var service_type = Number(e.detail.value);
    console.log('转换后的数据类型为', typeof service_type);
    this.setData({
      type: service_type
    })
  },
  // 输入退/换货原因
  reasonInput(e) {
    this.setData({
      reason: e.detail.value
    })
  },
  upload() {
    let self = this
    let arr = self.data.img_list

    wx.chooseImage({
      success: function (res) {
        if (arr.length >= 9) {
          wx.showToast({
            title: '最多只能选9张图片哦',
            image: '../../image/error.png'
          })
        } else {
          let img = res.tempFilePaths

          self.setData({
            img_list: arr.concat(img)
          })
        }
      }
    })
  },
  showImg(e) {
    let self = this
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: self.data.img_list
    })
  },
  submit() {
    wx.showLoading({
      title: '正在提交...',
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      // order_id = e.currentTarget.dataset.id,
      order_id = this.data.order_id,
      freight_num = this.data.freight_num,
      type = this.data.type,
      reason = this.data.reason,
      exchange_images = this.data.exchange_images
      self = this;
      
    // 上传图片
    this.uploadImage(exchange_images => {
      wx.request({
        url: baseUrl + '/MyOrder/customer_service',
        method: 'post',
        header,
        data: { access_token, order_id, freight_num, type, reason, exchange_images},
        success: res => {
          console.log(res.data, '****************');
          setTimeout(function () {
            wx.hideLoading();
          }, 3000);
          if (res.data.code == 200) {

            wx.navigateTo({
              url: '../my_order/my_order?active=4'
            })
          
              wx.showToast({
                title: '申请售后服务成功！',
                icon: 'success',
                duration: 10000,
              }) 

              // self.setData({
              //   status: 6
              // })    
          }else{
            wx.showToast({
              title: res.data.message,
              duration: 10000,
            })    
          }
        }
      })
    })
  },
  uploadImage(callback) {
    let self = this,
      baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      tempFilePaths = this.data.img_list
    console.log(tempFilePaths)
    function uploadTask(path, obj, index) {
      return new Promise((resolve, reject) => {
        const uploadTask_ = wx.uploadFile({
          url: baseUrl + '/MyOrder/upload_images', //仅为示例，非真实的接口地址
          filePath: path,
          name: 'exchange_images',
          formData: { access_token },
          success: function (res) {
            res.data = JSON.parse(res.data)
            if (res.data.code == 200) {
              obj = obj || {}
              obj[index] = res.data.data
              resolve(obj)
            } else {
              reject()
            }
          },
          fail(res) {
            console.log(res, '*************')
          }
        })
      })
    }
    var obj = {}
    var uploadImage = ''
    var promises = tempFilePaths.map((path, index) => {
      if (path.indexOf('wxfile') == 0) {
        return uploadTask(path, obj, index)
      } else {
        path = path.split('upload/')[1]
        index == 0 ? (uploadImage += path) : (uploadImage += ',' + path)
      }
    })
    Promise.all(promises).then(res => {
      console.log(obj)
      for (let i in obj) {
        i == 0 ? (uploadImage += obj[i]) : (uploadImage += ',' + obj[i])
      }
      callback(uploadImage)
    }).catch(res => {
      console.log(res)
    })

  },
  del(e) {
    let self = this
    wx.showActionSheet({
      itemList: ['删除'],
      success: function (res) {
        if (res.tapIndex == 0) {
          // console.log(e.currentTarget.dataset.index)
          let index = e.currentTarget.dataset.index
          let arr = self.data.img_list
          arr.splice(index, 1)
          self.setData({
            img_list: arr
          })
        }
      }
    })
  },
  
  // 
  onLoad(options) {
    let self = this
    if (options.id) {
      this.setData({
        options: options.id
      }, function () {
        self.getProductImg()
      })

    } else if (options.order_id) {
      this.setData({
        order_id: options.order_id
      })
    }
  },
  onPullDownRefresh: function () {
    this.getData();
    wx.stopPullDownRefresh()
  },

})